package wq.gdky005.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;

/**
 * 检测网络的工具类
 * @author wangqing
 * 2013-5-27
 */
public class CheckNetWorkStatus {

	/**
	 * 检查网络是否连通
	 * 
	 * @return true or false
	 */
	public static boolean isNetworkAvailable(Context context) {

		// 暂时不控制2G/3G
		return isNetworkAvailable(context, false);
	}

	/**
	 * 是否禁用2g 3g网,只有是wify连接才可以是有效连接
	 * 
	 * @author zivy 2012-11-17 下午2:28:48
	 * @param context
	 * @param isforbid2Gor3G
	 * @return
	 */
	public static boolean isNetworkAvailable(Context context,
			Boolean isforbid2Gor3G) {
		Boolean returnV = false;
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (connectivity == null) {
			return false;
		} else {
			// 获取代表联网状态的NetWorkInfo对象
			NetworkInfo info = connectivity.getActiveNetworkInfo();
			if (info == null) {
				return false;
			}
			boolean available = info.isAvailable();
			if (isforbid2Gor3G) {
				// 获取当前的网络连接是否可用

				// State state =
				// connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
				// if(State.CONNECTED==state){
				// Log.i("通知", "GPRS网络已连接");
				// }
				State state = connectivity.getNetworkInfo(
						ConnectivityManager.TYPE_WIFI).getState();
				if (State.CONNECTED == state && available) {
					returnV = true;
				}

				// 跳转到无线网络设置界面
				// startActivity(new
				// Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
				// // 跳转到无限wifi网络设置界面
				// startActivity(new
				// Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
				//
				//

			} else {

				// NetworkInfo[] info = connectivity.getAllNetworkInfo();
				// if (info != null) {
				// for (int i = 0; i < info.length; i++) {
				// if (info[i].getState() == NetworkInfo.State.CONNECTED) {
				// return true;
				// }
				// }
				// }

				returnV = available;

			}
		}
		return returnV;

	}
}
